﻿/**
* CRL
*/
using CRL.Core.Log;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace CRL.Core
{
    /// <summary>
    /// 写日志
    /// </summary>
	public class EventLog
    {
        static ThreadWork thread;
        static EventLog()
        {
            thread = new ThreadWork();
            thread.Start("eventLog", () =>
            {
                WriteLogFromCache();
                return true;
            }, 0.3);
        }

        internal static LogWriter __LogWriter = new LogWriter();
        public static void SetLogLogWriter(LogWriter logWriter)
        {
            __LogWriter = logWriter;
        }

        /// <summary>
        /// 自定义文件名前辍写入日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public static bool Log(string message, string typeName)
        {
            LogItem logItem = new LogItem();
            logItem.TypeName = typeName;
            logItem.Detail = message;
            return WriteLog(logItem);
        }

        /// <summary>
        /// 生成日志,默认文件名
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool Log(string message)
        {
            return Log(message, "");
        }
        public static bool Log(LogItem logItem, string folder)
        {
            logItem.Folder = folder;
            return WriteLog(logItem);
        }
        /// <summary>
        /// 生成日志,文件名以Error开头
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool Error(string message)
        {
            return Log(message, "Error");
        }
        /// <summary>
        /// 生成日志,文件名以Info开头
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool Info(string message)
        {
            return Log(message, "Info");
        }
        /// <summary>
        /// 生成日志,文件名以Debug开头
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool Debug(string message)
        {
            return Log(message, "Debug");
        }

        static ConcurrentQueue<LogItem> logQueue = new ConcurrentQueue<LogItem>();

        static bool WriteLog(LogItem logItem)//建立日志
        {
            if (__LogWriter.AsyncWrite)
            {
                var id = System.Guid.NewGuid().ToString("N");
                logItem.Id = id;
                logQueue.Enqueue(logItem);
            }
            else
            {
                __LogWriter.Write(new LogItem[] { logItem });
            }
            return true;
        }
        //public static string LastError;
        static void WriteLogFromCache()
        {
            //累加上次记录的日志
            if (logQueue.Any())
            {
                var list = new List<LogItem>();
                var i = 0;
                while (logQueue.Any() && i < 1000)
                {
                    var a = logQueue.TryDequeue(out var obj);
                    if (a)
                    {
                        list.Add(obj);
                        i += 1;
                    }
                }
                var group = list.GroupBy(b => new { b.Folder, b.TypeName });
                foreach (var g in group)
                {
                    var path = g.Key;
                    try
                    {
                        __LogWriter.Write(g.ToArray());
                    }
                    catch (Exception ero)
                    {
                        Console.WriteLine(ero.Message);
                        //LastError = ero.ToString();
                    }
                }
                list = null;
            }

        }
    }
}
